/*
 * table_valueInfo.c
 *
 *  Created on: 2012/04/24
 *      Author: guanxiaoying
 */
#include "../../parser/coredef.h"
#include "../../parser/cc/cc.h"

typedef struct _VALUETABLEINFO
{
	VALUEINFO value;
	RBTREE rbtree;
} VALUETBLINFO, *PVALUETBLINFO;

static AST gblValueTblInfo=0;

#define TableSize  (sizeof(VALUETBLINFO ))
#define TableType  PVALUETBLINFO
#define stTable stValueInfoManager.memoryBlocks
#define maxTable stValueInfoManager.maxRecordNum
#define  maxNumPerBlk  (stValueInfoManager.astMaxNum_Block)

static TABLEMANAGER stValueInfoManager = {
//		U4                  usIdx;
		0,
//		PSZ				    con_pszName;    constant
		__FILE__,
//		U2					con_usSize;		constant
		TableSize,
//		U2					block_num;	　　　　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_Block;     １回設定のみ
		0,
//		S4					astTabNum_LastPage; 　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_LastPage;     Reset clear  際　要
		0,
//		U4					ulMaxByte;	　　　　　　　　　　クリアしない
		0,
//		U4					con_minPageSize;　　　　　　　　　　constant
		0
};
static VOID resetListener( U4  procid, U2 index )
{
	if( procid == stValueInfoManager.u4Idx )
	{
		if( index > 0 && index < MAX_VOLUMN_OF_TABLE )
		{
			stTable[ index-1] = NULL;
		}
	}
}
static TableType core_getTable( AST index )
{
	TableType temp = (TableType)Check_Com( &stValueInfoManager, resetListener,  index );
	if( temp != NULL ){ return &temp[index%maxNumPerBlk]; } return NULL;
}
static BOOL ClearTable( VOID )
{
	gblValueTblInfo = 0;
	return Clear_Com( &stValueInfoManager );
}
static BOOL ResetTable( VOID )
{
	gblValueTblInfo = 0;
	return Reset_Com( &stValueInfoManager );
}
static BOOL AllocTable( AST astReqArg )
{
	return AllocTable_Com( &stValueInfoManager, astReqArg, resetListener );
}
/**************************************************************************
* 関 数 名 : copyTable
* 機    能 : 同じテーブルの行のブロックを移動する
* 引 き 数 ：
* 　　　　　　　astOrg :コピー元の開始行の連番
* 		　astDst：　コピー先の開始行の連番
*         astlenth: ブロック中の行の数
* 戻 り 値 : 成功する場合、1。失敗する場合、0
* 詳細内容 :  メモリ移動
************************************************************************/
static BOOL copyTable( AST  astOrg,  AST  astDst,  AST astLenth )
{
	return Copy_Com( &stValueInfoManager, resetListener, astOrg, astDst, astLenth );
}

static AST getMaxLineNum( VOID )
{
	return maxTable;
}
static U2  getU2( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->value.unValue.usval ;
}
/* set method */
static U2  setU2( AST index, U2 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->value.unValue.usval = value;
}
static S2  getS2( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->value.unValue.sval ;
}
/* set method */
static S2  setS2( AST index, S2 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->value.unValue.sval = value;
}
static U4  getU4( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->value.unValue.ulVal ;
}
/* set method */
static U4  setU4( AST index, U4 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->value.unValue.ulVal = value;
}
static S4  getS4( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->value.unValue.lVal ;
}
/* set method */
static S4  setS4( AST index, S4 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->value.unValue.lVal = value;
}
static S8  getS8( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->value.unValue.llval ;
}
/* set method */
static S8  setS8( AST index, S8 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->value.unValue.llval = value;
}
static U8  getU8( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->value.unValue.ullval ;
}
/* set method */
static U8  setU8( AST index, U8 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->value.unValue.ullval = value;
}
static DOUBLE  getDb( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->value.unValue.dbVal ;
}
/* set method */
static DOUBLE  setDb( AST index, DOUBLE value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->value.unValue.dbVal = value;
}
static AST  getType( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->value.astType ;
}
/* set method */
static AST  setType( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->value.astType  = value;
}
static AST getLeft( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astLeftChild;
}
static AST setLeft( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astLeftChild = value;
}
static AST getRight( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astRightChild;
}
static AST setRight( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astRightChild = value;
}
static AST getParent( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astParent;
}
static AST setParent( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astParent = value;
}
static U2 getColor( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.lColor;
}
static U2 setColor( AST index, U2 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.lColor = value;
}

static AST  getIndex( AST index )
{
	return index;
}
static ACCESS_RBTREE stAccessRbtree =
{
		AllocTable,
		getMaxLineNum,
		getIndex,
		{
				getLeft,
				setLeft
		},
		{
				getRight,
				setRight
		},
		{
				getParent,
				setParent
		},
		{
				getColor,
				setColor
		}
};
static S4  cmpValueInfo( AST astValue, void * pValue )
{
	PVALUEINFO pst = ( PVALUEINFO )pValue;
	S4 ret;
	TableType pTbl;
	if( pst == NULL ){
		return RBT_FOUND;
	}
	if( astValue == 0 ){
		return RBT_LESS;
	}
	pTbl = core_getTable( astValue );
	if( pTbl == NULL )
	{
		return RBT_LESS;
	}
	ret = CcValueCompareForTree( pst, &pTbl->value );
	if( ret < 0 )
	{
		return RBT_LESS;
	}
	if( ret > 0 )
	{
		return RBT_LARGER;
	}
	return RBT_FOUND;
}
static AST addValueInfo( PVALUEINFO pValue )
{
	AST astResult;
	S4 find_result;
	AST nMaxIndex=0;
	BOOL bret;
	TableType pTbl;
	if( pValue == NULL ){
		return 0;
	}
	find_result = FindData( &stAccessRbtree, gblValueTblInfo, pValue, &astResult, cmpValueInfo );
	if( find_result == RBT_FOUND )
	{
		return astResult;
	}
	bret =  InsertData( &stAccessRbtree, find_result, astResult, &gblValueTblInfo, &nMaxIndex );
	if( bret == FALSE  )
	{
		return (COEXIT_FATAL);
	}
	pTbl = core_getTable( nMaxIndex );
	if( pTbl == NULL )
	{
		return COEXIT_FATAL;
	}
	pTbl->value = *pValue;
	return nMaxIndex;
}
DOUBLE  (*getDoubleValue)(AST);
const VALUEINFO_ACCESS stValueInfoAccess ={
//		BOOL (*clear)( VOID );
		ClearTable,
//		BOOL (*reset)( VOID );
		ResetTable,
//		BOOL (*alloc)( AST );
		AllocTable,
//		BOOL (*copy)( AST, AST, AST );
		copyTable,
//		AST  (*getMax)( VOID );
		getMaxLineNum,
//		AST  (*addValue)(PVALUEINFO);
		addValueInfo,
//		ACCESS_IDX_FIELD type;
		{
				getType,
				setType
		},
//		ACCESS_S2_FIELD s2value;
		{
				getS2,
				setS2
		},
//		ACCESS_U2_FIELD u2value;
		{
				getU2,
				setU2
		},
//		ACCESS_U4_FIELD u4value;
		{
				getU4,
				setU4
		},
//		ACCESS_S4_FIELD s4value;
		{
				getS4,
				setS4
		},
		{
				getU8,
				setU8
		},
		{
				getS8,
				setS8
		},
		//		ACCESS_DOUBLE_FIELD dvalue;
		{
				getDb,
				setDb
		}
};
